package uvt.gui.mainframe.object.memento;

import javax.swing.JButton;

import uvt.database.Database;
import uvt.database.DeleteOperations;
import uvt.gui.mainframe.object.InfoFrame;
import uvt.gui.mainframe.object.command.Command;
import uvt.gui.mainframe.object.memento.container.ButtonOperationBackup;
import uvt.gui.mainframe.object.memento.container.Container;
import uvt.gui.mainframe.object.observer.ConcreteObservable;

public class UndoOperationButton extends JButton implements Command {
	
	private ButtonOperations operation;
	
	public UndoOperationButton(ButtonOperations op) {
		super("Undo");
		setEnabled(false);
		operation = op;
	}
	
	public void execute() {
		int id = operation.getId();
		Container container = ButtonOperationBackup.getInstance().
				              getPreviousOperationToCareTakerFromReference(operation).
				              getContainer();
		if(id == 0) {
			((DeleteOperations)Database.getInstace()).deleteVehicle(container.getName());
			ConcreteObservable.getVehiclePanelObservableInstance().stateChanged();
		} else if(id == 1) {
			((DeleteOperations)Database.getInstace()).deleteRouteName(container.getName());
		} else if(id == 2) {
			
		}
		setEnabled(false);
		Thread t = new Thread(new Runnable() {
			public void run() {
				new InfoFrame("Information","Operation undo succeded!");
			}
		});
		t.start();
	}

}
